package com.design.graduation.asdemo.controller;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

import com.design.graduation.asdemo.Constant;
import com.design.graduation.asdemo.R;
import com.design.graduation.asdemo.base.ActionBarActivity;
import com.design.graduation.asdemo.manager.UserManager;
import com.design.graduation.asdemo.model.Test;
import com.design.graduation.asdemo.network.HSFObserver;
import com.design.graduation.asdemo.network.model.LoginResult;
import com.design.graduation.asdemo.service.DBService;
import com.design.graduation.asdemo.utils.PreferenceUtils;
import com.design.graduation.asdemo.utils.ToastUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.commons.lang3.StringUtils;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by lijinyuan on 16/2/19.
 */
@EActivity(R.layout.activity_login)
public class LoginActivity extends ActionBarActivity implements TextWatcher {
    private String uid;
    @ViewById(R.id.login_button)
    Button loginButton;
    @ViewById(R.id.register_button)
    Button register;
    @ViewById(R.id.login_phone_no)
    EditText editNo;

    @ViewById(R.id.login_code)
    EditText editPassword;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DBService.insertOrReplaceTest(new Test(Long.valueOf(20), "ss", "dd"));
        uid = JPushInterface.getRegistrationID(this);
        Log.d("tag",uid);
    }

    @AfterViews
    protected void init() {
        setToolBar();
    }


    @Click(R.id.login_button)
    void login() {
        if (!isValidCode()) {
            ToastUtil.showLongToast("信息有误");
        } else {
            String id = editNo.getText().toString();
            String password = editPassword.getText().toString();
            UserManager.RxLogin(id, password, new HSFObserver<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    ToastUtil.showLongToast(loginResult.getMessage());
                    if (loginResult.getStatus() == 200) {
                        Intent intent = new Intent(LoginActivity.this, PersonalActivity_.class);
                        startActivity(intent);
                        PreferenceUtils.setBoolean(Constant.isCert, true);
                    }
                }
            });
        }


    }


    @Click(R.id.register_button)
    void register() {
        if (!isValidCode()) {
            ToastUtil.showLongToast("信息有误");
        } else {
            String id = editNo.getText().toString();
            String password = editPassword.getText().toString();
            UserManager.RxRegister(id, password, uid, new HSFObserver<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    ToastUtil.showLongToast(loginResult.getMessage());
                    if (loginResult.getStatus() == 200) {
                        Intent intent = new Intent(LoginActivity.this, PersonalActivity_.class);
                        startActivity(intent);
                        finish();
                        PreferenceUtils.setBoolean(Constant.isCert, true);
                    }
                }
            });
        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem text = menu.findItem(R.id.action_text);
        MenuItem img = menu.findItem(R.id.action_img);
        text.setVisible(false);
        img.setVisible(false);
        return true;
    }

    private void setToolBar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("登陆");
        setSupportActionBar(toolbar);

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        loginButton.setEnabled(isValidCode());
        register.setEnabled(isValidCode());

    }

    @Override
    public void afterTextChanged(Editable s) {

    }


    private boolean isValidCode() {
        String no = editNo.getText().toString().trim();
        String password = editPassword.getText().toString().trim();
        if (no.length() != 11 || StringUtils.isBlank(no)) {
            return false;
        } else if (password.length() != 4 || StringUtils.isBlank(password)) {
            return false;
        } else {
            return true;
        }


    }
}
